const { DataTypes } = require('sequelize');
const sequelize = require('../db/connect');

// 定义 Menu 模型
const Menu = sequelize.define('Menu', {
    id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    name: {
        type: DataTypes.STRING,
        allowNull: false
    },
    menu_code: {
        type: DataTypes.STRING,
        allowNull: false
    },
    parent_id: {
        type: DataTypes.INTEGER,
        allowNull: true
    },
    nodeType: {
        type: DataTypes.INTEGER,
        allowNull: false
    },
    icon: {
        type: DataTypes.STRING,
        allowNull: true
    },
    link_url: {
        type: DataTypes.STRING,
        allowNull: true
    },
    level: {
        type: DataTypes.INTEGER,
        allowNull: true
    },
    createdAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW
    },
    updatedAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW,
        updataValue: DataTypes.NOW
    }
});

// 定义 Role 模型
const Role = sequelize.define('Role', {
    id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    name: {
        type: DataTypes.STRING,
        allowNull: false
    },
    createdAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW
    },
    updatedAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW,
        updataValue: DataTypes.NOW
    }
});

// 定义 User 模型
const User = sequelize.define('User', {
    id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    username: {
        type: DataTypes.STRING,
        allowNull: false
    },
    password: {
        type: DataTypes.STRING,
        allowNull: false
    },
    avatar: {
        type: DataTypes.STRING,
        allowNull: true
    },
    introduction: {
        type: DataTypes.STRING,
        allowNull: true
    },
    name: {
        type: DataTypes.STRING,
        allowNull: true
    },
    createdAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW
    },
    updatedAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW,
        updataValue: DataTypes.NOW
    }
});

// 定义多对多关系
Menu.belongsToMany(Role, { through: 'Menu_role' });
Role.belongsToMany(Menu, { through: 'Menu_role' });
Role.belongsToMany(User, { through: 'User_role' });
User.belongsToMany(Role, { through: 'User_role' });


function childrenMenu(MenuList) {
    let menus = [];
    MenuList.forEach(menu => {
        if (menu.level === 1) {
            let children = MenuList.filter(item => item.parent_id === menu.id);
            if (children.length > 0) {
                menu.children = children;
            }
            menus.push(menu);
        }
    });
    return menus;
}

// 查询用户所含有的角色以及对应菜单的关系
async function getUserRoleMenu(userId) {
    let user = await User.findByPk(userId, {
        include: {
            model: Role,
            include: Menu
        },
    });
    user = JSON.parse(JSON.stringify(user));
    if (user && user.Roles) {
        let menus = [];
        user.Roles.forEach(role => {
            role.Menus.forEach(menu => {
                if (!menus.find(item => item.id === menu.id)) {
                    // 删除多余的属性
                    delete menu.Menu_role;
                    menus.push(menu);
                }
            });
        });

        let res = {
            id: user.id,
            username: user.username,
            avatar: user.avatar,
            introduction: user.introduction,
            menus: childrenMenu(menus)
        };

        return res;
    }
    return null;
}


// 查询用户所含角色所含权限
async function getUserRole(userId) {
    let user = await User.findByPk(userId, {
        include: {
            model: Role,
            include: Menu
        },
    });
    let menus = [];
    user = JSON.parse(JSON.stringify(user));
    if (user && user.Roles) {
        user.Roles.forEach(role => {
            role.Menus.forEach(menu => {
                if (!menus.find(item => item.id === menu.id)) {
                    // 删除多余的属性
                    delete menu.Menu_role;

                    menus.push(menu);
                }
            });
        });
    }


    return menus;

}

module.exports = { Menu, Role, User, sequelize, getUserRoleMenu, getUserRole };